<template>
  <div>
    <common-head title="所有联系人"></common-head>
    <div class="wrap">
      <van-address-list
        v-model="chosenAddressId"
        :list="contacts"
        default-tag-text="默认"
        @add="onAdd"
        @edit="onEdit"
        @select="changeContact"
      />
    </div>
  </div>
</template>

<script>
import CommonHead from 'components/CommonHead'
import { mapState, mapMutations } from 'vuex'
export default {
  data () {
    return {
      chosenAddressId: '1'
    }
  },
  created () {
    this.setDefaultId()
  },
  methods: {
    changeContact (item, index) {
      // 切换地址
      console.log(item)
      this.setOrderContact(item)
    },
    setDefaultId () {
      const defaultContact = this.contacts.find(el=>el.isDefault)
      if (defaultContact) {
         this.chosenAddressId = defaultContact.id
      } else {
         this.chosenAddressId = this.contacts[0].id
      }
     
    },
    onAdd () {
      // 点击新增地址按钮
      this.$router.push('/contactAdd')
    },
    onEdit (item, index) {
      // 点击编辑按钮
      this.$router.push(`/contactEdit/${index}`)
    },
    ...mapMutations('order', ['setOrderContact'])
  },
  computed: {
    ...mapState({
      contacts: state => state.contact.contacts
    })
  },
  components: {
    CommonHead
  }
}
</script>

<style lang="scss" scoped>

</style>